{% extends "layout.html" %}
{% block content %}
    <div class="container">
        <div style="padding-bottom: 10px">
            <input type="button" class="btn btn-primary" value="新建订单1" data-toggle="modal" data-target="#myModal">
            <input id="btnAdd" type="button" class="btn btn-primary" value="新建订单2">
        </div>
        <div>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
                    订单列表
                </div>
                <table class="table table-bordered">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>订单编号</th>
                        <th>商品名称</th>
                        <th>价格</th>
                        <th>状态</th>
                        <th>管理员</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for order in queryset %}
                        <tr>
                            <th>{{ order.id }}</th>
                            <th>{{ order.oid }}</th>
                            <th>{{ order.title }}</th>
                            <th>{{ order.price }}</th>
                            <td>{{ order.get_status_display }}</td>
                            <td>{{ order.admin.username }}</td>
                            <td>
                                <input uid="{{ order.id }}" type="button" class="btn-primary btn-xs btn-edit"
                                       value="编辑">
                                <input uid="{{ order.id }}" class="btn-danger btn-xs btn-delete" type="button"
                                       value="删除">
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
        {{ page_html }}
    </div>
    <!-- 新建订单Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">新建</h4>
                </div>
                <div class="modal-body">
                    <form id="formAdd">
                        <div class="clearfix">
                            {% for field in form %}
                                <div class="col-xs-6">
                                    <div class="form-group" style="position:relative; margin-bottom:20px">
                                        <label>{{ field.label }}</label>
                                        {{ field }}
                                        <span class="error_msg" style="color:darkred;position:absolute"></span>
                                    </div>
                                </div>
                            {% endfor %}
                        </div>
                    </form>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button id="btnSave" type="button" class="btn btn-primary">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- 删除Modal -->
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="alert alert-danger alert-dismissible fade in" role="alert">
                <h4>是否确定删除？</h4>
                <p style="margin:10px 0;">确认后所有相关数据都将被删除！！</p>
                <p style="text-align: right">
                    <button id="btnDeleteConfirm" type="button" class="btn btn-danger btn-delete">确定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </p>
            </div>
        </div>
    </div>
{% endblock %}

{% block js %}
    <script type="text/javascript">
        var DELETE_ID;
        var EDIT_ID = undefined;

        $(function () {
            bindBtnAddEvent();
            bindBtnSaveEvent();
            bindBtnDeleteEvent();
            bindBtnEditEvent();
            bindBtnDeleteConfirmEvent();
        });

        //编辑订单
        function bindBtnEditEvent() {
            $('.btn-edit').click(function () {
                //清空对话框中的数据
                $(".error_msg").empty();
                $('#formAdd')[0].reset();
                EDIT_ID = $(this).attr('uid');
                $.ajax({
                    url: "/order/detail/",
                    type: 'get',
                    data: {
                        uid: EDIT_ID
                    },
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            //console.log(res);
                            $.each(res.data, function (name, value) {
                                $('#id_' + name).val(value);
                            });
                            $('#myModalLabel').text('编辑');
                            $('#myModal').modal('show');
                        } else {
                            alert(res.errors);
                        }
                    }
                });
            });
        }

        //确定删除订单
        function bindBtnDeleteConfirmEvent() {
            $("#btnDeleteConfirm").click(function () {
                $.ajax({
                    url: "/order/delete/",
                    type: "get",
                    data: {
                        uid: DELETE_ID
                    },
                    dataType: "JSON",
                    success: function (res) {
                        //parsedRes = $.parseJSON(res)
                        //if (parsedRes['status']) {
                        if (res.status) {
                            $('#deleteModal').modal('hide');
                            DELETE_ID = 0;
                            alert("删除成功！");
                            location.reload();
                        } else {
                            alert(res.errors);
                        }
                    }
                });
            });
        }

        //删除订单
        function bindBtnDeleteEvent() {
            $(".btn-delete").click(function () {
                $('#deleteModal').modal('show');
                DELETE_ID = $(this).attr('uid');
            });
        }

        //新建订单
        function bindBtnAddEvent() {
            $("#btnAdd").click(function () {
                //将EDIT_ID置空
                EDIT_ID = undefined;
                //清空对话框中的数据
                $(".error_msg").empty();
                $('#formAdd')[0].reset();
                $('#myModalLabel').text('新建');
                $('#myModal').modal('show');
            });
        }

        //保存新建|编辑订单
        function bindBtnSaveEvent() {
            $("#btnSave").click(
                function () {
                    $(".error_msg").empty();
                    console.log(EDIT_ID);
                    if (EDIT_ID) {
                        //console.log('edit');
                        doEdit();
                    } else {
                        //console.log('add');
                        doAdd();
                    }
                });
        }

        //编辑订单
        function doEdit() {
            $.ajax({
                url: "/order/edit/?uid=" + EDIT_ID,
                type: "post",
                data: $("#formAdd").serialize(),
                success: function (res) {
                    //解析JSON字符串
                    //parsedRes = $.parseJSON(res)
                    //console.log(res);
                    if (res.status) {
                        alert('数据编辑成功！');
                        $('#myModal').modal('hide');
                        //刷新页面
                        location.reload();
                    } else {
                        if (res.tips) {
                            alert(res.tips);
                            $('#myModal').modal('hide');
                        }
                        if (res.errors) {
                            $.each(res.errors, function (name, data) {
                                    $("#id_" + name).next().text(data[0]);
                                }
                            );
                        }
                    }
                }
            });
        }

        //新建订单
        function doAdd() {
            $.ajax({
                url: "/order/add/",
                type: "post",
                data: $("#formAdd").serialize(),
                success: function (res) {
                    {#解析JSON字符串#}
                    parsedRes = $.parseJSON(res)
                    if (parsedRes['status']) {
                        //alert('数据添加成功！');
                        //清空表单,$('#formAdd')为jQuery对象,$('#formAdd')[0] DOM对象
                        $("#formAdd")[0].reset();
                        $('#myModal').modal('hide');
                        //刷新页面
                        location.reload();
                    } else {
                        errorObj = $.parseJSON(res)['errors'];
                        $.each(errorObj, function (name, data) {
                                $("#id_" + name).next().text(data[0]);
                            }
                        );
                    }
                }
            });
        }
    </script>
{% endblock %}